import multiparty from 'multiparty'
import { Request } from 'express'
import { unlinkSync } from 'fs-extra';
import { resolve } from '../../utils/common';

/**
 * 根据绝对地址获得文件名
 * @param avatarUrl
 */
export const getAvatarUrl = (avatarUrl: string) => {
    const i = avatarUrl.lastIndexOf('/');
    return avatarUrl.slice(i + 1);
};


/**
 * 解析上传的文件
 * @param req 
 * @param cb 
 */
export const resolveFile = (req: Request, cb: Function) => {
    const form = new multiparty.Form();
    form.parse(req, async (_err: Error, _fileds: any, files: any) => {
        cb(files)
    })
}

/**
 * 删除图片
 * @param dir 
 * @param name 
 */
export const unlinkFile = (dir: string, name: string) => unlinkSync(resolve(`../public/${dir}/${name}`))
